from typing import Optional, List
from pydantic import BaseModel, Field

from app.schemas.base import BaseSchema, TimestampMixin, TenantMixin


class PermissionCreate(BaseModel):
    """创建权限"""
    name: str = Field(..., min_length=1, max_length=50, description="权限名称")
    code: str = Field(..., min_length=1, max_length=100, description="权限编码")
    type: str = Field("M", pattern=r"^[MCF]$", description="权限类型: M-目录, C-菜单, F-按钮")
    parent_id: int = Field(0, description="父权限ID")
    path: Optional[str] = Field(None, max_length=200, description="路由地址")
    component: Optional[str] = Field(None, max_length=255, description="组件路径")
    perms: Optional[str] = Field(None, max_length=100, description="权限标识")
    icon: Optional[str] = Field(None, max_length=100, description="菜单图标")
    sort: int = Field(0, description="显示顺序")
    is_frame: bool = Field(False, description="是否为外链")
    is_cache: bool = Field(False, description="是否缓存")
    visible: bool = Field(True, description="显示状态")
    status: bool = Field(True, description="权限状态")
    remark: Optional[str] = Field(None, max_length=500, description="备注")


class PermissionUpdate(BaseModel):
    """更新权限"""
    name: Optional[str] = Field(None, min_length=1, max_length=50, description="权限名称")
    code: Optional[str] = Field(None, min_length=1, max_length=100, description="权限编码")
    type: Optional[str] = Field(None, pattern=r"^[MCF]$", description="权限类型")
    parent_id: Optional[int] = Field(None, description="父权限ID")
    path: Optional[str] = Field(None, max_length=200, description="路由地址")
    component: Optional[str] = Field(None, max_length=255, description="组件路径")
    perms: Optional[str] = Field(None, max_length=100, description="权限标识")
    icon: Optional[str] = Field(None, max_length=100, description="菜单图标")
    sort: Optional[int] = Field(None, description="显示顺序")
    is_frame: Optional[bool] = Field(None, description="是否为外链")
    is_cache: Optional[bool] = Field(None, description="是否缓存")
    visible: Optional[bool] = Field(None, description="显示状态")
    status: Optional[bool] = Field(None, description="权限状态")
    remark: Optional[str] = Field(None, max_length=500, description="备注")


class PermissionResponse(BaseSchema, TimestampMixin, TenantMixin):
    """权限响应"""
    id: int
    name: str
    code: str
    type: str
    parent_id: int
    path: Optional[str] = None
    component: Optional[str] = None
    perms: Optional[str] = None
    icon: Optional[str] = None
    sort: int
    is_frame: bool
    is_cache: bool
    visible: bool
    status: bool
    remark: Optional[str] = None
    children: List["PermissionResponse"] = []


class PermissionTreeResponse(BaseModel):
    """权限树响应"""
    permissions: List[PermissionResponse]


class MenuPermissionResponse(BaseModel):
    """菜单权限响应"""
    id: int
    name: str
    path: Optional[str] = None
    component: Optional[str] = None
    icon: Optional[str] = None
    sort: int
    is_frame: bool
    is_cache: bool
    visible: bool
    children: List["MenuPermissionResponse"] = []


# 解决前向引用问题
PermissionResponse.model_rebuild()
MenuPermissionResponse.model_rebuild()